package com.lanyam.travel.base.service.impl;


import com.lanyam.travel.base.entity.DistrTeam;
import com.lanyam.travel.base.entity.vo.DistrTeamVo;
import com.lanyam.travel.base.dao.DistrTeamMapper;
import com.lanyam.travel.base.service.IDistrTeamService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;


@Service
@Transactional
@Slf4j
public class DistrTeamServiceImpl implements IDistrTeamService {

    @Resource
    private DistrTeamMapper distrTeamMapper;

    @Override
    public IPage<DistrTeamVo> getPageDistrTeam (Map<String, Object> queryMap, Integer page, Integer rows) {
        Page<DistrTeamVo> pageSize = new Page<DistrTeamVo>(page, rows);
        Page<DistrTeamVo> pageInfo = distrTeamMapper.getPage(pageSize, queryMap);
        return pageInfo;
    }

    @Override
    public void addDistrTeam(DistrTeam distrTeam) {
        distrTeamMapper.insert(distrTeam);
    }

    @Override
    public void updateDistrTeam(DistrTeam distrTeam) {
        distrTeamMapper.updateById(distrTeam);
    }

    @Override
    public void delById(String id) {
        distrTeamMapper.delById(id);
    }

    @Override
    public DistrTeamVo getAllTree(DistrTeamVo dv) {
        String root_id = dv.getId();
        Map<String, Object> queryMap  = new HashMap<>();
        queryMap.put("father_teamid",root_id);
        List<DistrTeam> childrenL =  distrTeamMapper.selectByMap(queryMap);
        List<DistrTeamVo> treeVo= new ArrayList<>();
        if(CollectionUtils.isNotEmpty(childrenL)){
            childrenL.forEach(x ->{
                DistrTeamVo dto= DistrTeamVo.byRootDistrTeamGetDto(x);
                dto = getAllTree(dto);
                treeVo.add(dto);
            });
        }
        dv.setChildren(treeVo);
        return dv;
    }
}